package com.itheima.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Dept;
import com.itheima.domain.system.Module;
import com.itheima.domain.system.Role;
import com.itheima.domain.system.User;
import com.itheima.utils.BeanUtil;
import com.itheima.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet("/system/user")
public class UserServlet extends BaseServlet {
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String operation = req.getParameter("operation");
        if ("list".equals(operation)) {
            findAll(req, resp);
        } else if ("toAdd".equals(operation)) {
            toAdd(req, resp);
        } else if ("save".equals(operation)) {
            save(req, resp);
        } else if ("toEdit".equals(operation)) {
            toEdit(req, resp);
        } else if ("edit".equals(operation)) {
            update(req, resp);
        } else if ("delete".equals(operation)) {
            delete(req, resp);
        }else if ("userRoleList".equals(operation)) {
            userRoleList(req, resp);
        }else if ("updateRole".equals(operation)) {
            updateRole(req, resp);
        }else if ("login".equals(operation)) {
            login(req, resp);
        }else if ("logout".equals(operation)) {
            logout(req, resp);
        }else if ("home".equals(operation)) {
            home(req, resp);
        }
//        else if ("main".equals(operation)) {
//            main(req, resp);
//        }
    }

//    private void main(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.getRequestDispatcher(req.getContextPath()+"/login.jsp").forward(req,resp);
//    }

    private void home(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/pages/home/home.jsp").forward(req,resp);
    }

    private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String email = req.getParameter("email");
        String password = req.getParameter("password");
        User user = userService.login(email, password);
        if(user==null){
            resp.getWriter().write("用户名/密码错误,请重新登录");
            resp.setHeader("refresh","1;url=../../login.jsp");
        }else{

            List<Module> modules = moduleService.findModuleByUserId(user.getId());
            req.getSession().setAttribute("modules",modules);
            user.setPassword("");
            req.getSession().setAttribute("loginUser",user);
            req.getRequestDispatcher("/WEB-INF/pages/home/main.jsp").forward(req,resp);
        }
    }

    private void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().removeAttribute("loginUser");
        resp.sendRedirect(req.getContextPath()+"/login.jsp");

    }

    private void updateRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userId = req.getParameter("userId");
        String[] roleIds = req.getParameterValues("roleIds");
        userService.updateRole(userId,roleIds);


        resp.sendRedirect(req.getContextPath()+"/system/user?operation=list");

    }

    private void userRoleList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        User user = userService.findById(id);
        System.out.println(id);
        req.setAttribute("user",user);
        List<Role> all = roleService.findByUserId(id);
        req.setAttribute("roleList",all);


        req.getRequestDispatcher("/WEB-INF/pages/system/user/role.jsp").forward(req,resp);

    }


    private void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int page = 1;
        int size = 5;
        if(StringUtils.isNoneBlank(req.getParameter("page"))){
            page= Integer.parseInt(req.getParameter("page"));
        }
        if(StringUtils.isNoneBlank(req.getParameter("size"))){
            size = Integer.parseInt(req.getParameter("size"));
        }
        PageInfo all = userService.findAll(page, size);
        req.setAttribute("page",all);
        req.getRequestDispatcher("/WEB-INF/pages/system/user/list.jsp").forward(req,resp);
    }

    private void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Dept> all = deptService.findAll();
        req.setAttribute("deptList",all);

        req.getRequestDispatcher("/WEB-INF/pages/system/user/add.jsp").forward(req,resp);
    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = BeanUtil.fillBean(req, User.class, "yyyy-MM-dd");

        userService.save(user);
        resp.sendRedirect(req.getContextPath()+"/system/user?operation=list");
    }

    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        String id = req.getParameter("id");
        User user = userService.findById(id);
        req.setAttribute("user",user);
        List<Dept> all = deptService.findAll();
        req.setAttribute("deptList",all);
        req.getRequestDispatcher("/WEB-INF/pages/system/user/update.jsp").forward(req,resp);

    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = BeanUtil.fillBean(req, User.class, "yyyy-MM-dd");
        userService.update(user);
        resp.sendRedirect(req.getContextPath()+"/system/user?operation=list");
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String[] id = req.getParameter("id").split(",");
        userService.delete(id);
        resp.sendRedirect(req.getContextPath()+"/system/user?operation=list");
    }


    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
